package com.servlet;


import com.dao.StudentDao;
import com.model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    private StudentDao studentDao = new StudentDao();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String account = req.getParameter("account");
        Student student = studentDao.selectByAccount(account);
        if (student == null){
            resp.getWriter().println("账号出错");
            resp.getWriter().close();
            return;
        }
        String stuPassword = student.getStuPassword();

        String password = req.getParameter("password");
        if (!stuPassword.equals((password))){
            resp.getWriter().println("密码错误");
            resp.getWriter().close();
            return;
        }
        HttpSession session = req.getSession();
        session.setAttribute("username",student.getStuName());

        resp.sendRedirect("main.jsp");
    }
}
